#include <ezfb.h>
#include <basic.h>

#include <string.h>

BOOL		EZFBAPI		WriteBlock(
						LONG x,
						LONG y,
						LONG cx,
						LONG cy,
						LPVOID lpBits,
						DWORD dwFlag
						) {
	LPSURFACE lpSurface	= (dwFlag & DF_BUFFER) ? (&sfBuffer) : (&sfScreen);
	if (x < 0 || y < 0 || x >= lpSurface->cx || y >= lpSurface->cy)
		return FALSE;
	if (((x * lpSurface->lnBitPerPixel) & 0x00000007) || ((cx * lpSurface->lnBitPerPixel) & 0x00000007))
		return FALSE;
	LPVOID lpPixel	= (LPVOID)((LPBYTE)lpSurface->lpBits + y * lpSurface->lnBytePerLine + (x * lpSurface->lnBitPerPixel / 8));
	LONG lnBytePerLine	= cx * lpSurface->lnBitPerPixel / 8;
	LONG line;
	for (line = 0; line < cy; ++ line) {
		memcpy(lpPixel, lpBits, lnBytePerLine);
		lpPixel += lpSurface->lnBytePerLine;
		lpBits	+= lnBytePerLine;
	}
	return TRUE;
}

BOOL		EZFBAPI		GetBlock(
						LONG x,
						LONG y,
						LONG cx,
						LONG cy,
						LPVOID lpBits,
						DWORD dwFlag
						) {
	LPSURFACE lpSurface	= (dwFlag & DF_BUFFER) ? (&sfBuffer) : (&sfScreen);
	if (x < 0 || y < 0 || x >= lpSurface->cx || y >= lpSurface->cy)
		return FALSE;
	if (((x * lpSurface->lnBitPerPixel) & 0x00000007) || ((cx * lpSurface->lnBitPerPixel) & 0x00000007))
		return FALSE;
	LPVOID lpPixel	= (LPVOID)((LPBYTE)lpSurface->lpBits + y * lpSurface->lnBytePerLine + (x * lpSurface->lnBitPerPixel / 8));
	LONG lnBytePerLine	= cx * lpSurface->lnBitPerPixel / 8;
	LONG line;
	for (line = 0; line < cy; ++ line) {
		memcpy(lpBits, lpPixel, lnBytePerLine);
		lpPixel += lpSurface->lnBytePerLine;
		lpBits	+= lnBytePerLine;
	}
	return TRUE;
}
